package com.code.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.code.springboot.common.R;
import com.code.springboot.entity.Employee;
import com.code.springboot.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    /*员工登录*/
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //密码加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());


        //根据用户名查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);
        //判断查询，失败则登录失败
        if(emp == null){
            return R.error("请检查用户名是否正确");
        }
        if(!emp.getPassword().equals(password)){
            return R.error("密码错误");
        }
        if(emp.getStatus()!=1){
            return R.error("员工已离职");
        }
        //登录成功，将员工id存入Session并返回成功登录结果
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }


    //退出登录
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //清理session
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    //新增员工
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("新增员工：{}",employee.toString());

        //设置初始密码，md5加密后的123456
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);

        return R.success("新增员工成功");

    }

    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
        @GetMapping("/page")
        public R<Page> page(int page,int pageSize,String name){
        log.info("page={},pageSize={},name={}",page,pageSize,name);

        //构造分页构造器
        Page pageInfo = new Page();
        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();

        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);

        //执行查询
         employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
        }


        //修改员工信息
        @PutMapping
        public R<String> update(HttpServletRequest request, @RequestBody Employee employee){
//            Long empId = (long)request.getSession().getAttribute("employee");
             //   Long id = Thread.currentThread().getId();
//            employee.setUpdateUser(empId);
//            employee.setUpdateTime(LocalDateTime.now());
            employeeService.updateById(employee);

            return R.success("员工信息修改成功");
        }


    /**
     * 根据id查员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
            Employee employee = employeeService.getById(id);

            if(employee != null)
            {return R.success(employee);}
            return R.error("没查询到员工信息");
        }

}
